package cn.wolfcode.wolf2w.business.controller;

import cn.wolfcode.wolf2w.business.api.RemoteDestinationEsService;
import cn.wolfcode.wolf2w.business.api.domain.DestinationES;
import cn.wolfcode.wolf2w.business.api.domain.Region;
import cn.wolfcode.wolf2w.business.api.domain.StrategyES;
import cn.wolfcode.wolf2w.business.service.IRegionService;
import cn.wolfcode.wolf2w.common.core.constant.SecurityConstants;
import cn.wolfcode.wolf2w.common.core.domain.R;
import cn.wolfcode.wolf2w.common.security.annotation.InnerAuth;
import cn.wolfcode.wolf2w.business.api.domain.Destination;
import cn.wolfcode.wolf2w.business.query.DestinationQuery;
import cn.wolfcode.wolf2w.business.service.IDestinationService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.Map;

/**
 * 目的地 Controller
 *
 * @author zty
 * @date 2025-09-16
 */
@RestController
@RequestMapping("destinations")
public class DestinationController {
    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private IRegionService regionService;
    @Autowired
    private RemoteDestinationEsService remoteDestinationEsService;
    /**
     * 目的地详情
     */
    @GetMapping("/detail/{id}")
    public R<Destination> detail(@PathVariable Long id) {
        Destination destination = destinationService.getById(id);
        return R.ok(destination);
    }
    /**
     * 目的地 列表
     */
    @GetMapping("/query")
    public R<IPage<Destination>> query(DestinationQuery qo) {
        IPage<Destination> page = destinationService.queryPage(qo);
        return R.ok(page);
    }
    @GetMapping("/regions")
    public R<List<Region>> regins(Long ishot){
    List<Region> regions =  regionService.queryRegions(ishot);
        return  R.ok(regions);
    }
        /*
        自己练习写的
    @GetMapping("/search")
    public  R<List<Map<String, Object>>> search(Integer regionId){

     List<Map<String,Object>> destinations =  destinationService.search(regionId);

    return R.ok(destinations);

    }*/
    @GetMapping("/search")
    public R<List<Destination>> search(Long regionId){
        List<Destination> destinations = destinationService.queryByRegionId(regionId);
        return R.ok(destinations);
    }
    @GetMapping("/toasts")
    public R<List<Destination>> toasts(Long destId){
     List<Destination> destinations = destinationService.queryToasts(destId);
        return R.ok(destinations);
    }
    @GetMapping("/list")
    public R<List<Destination>> list() {
        return R.ok(destinationService.list());
    }

    /*****************************************对外暴露Fegin接口**********************************************/
    /**
     * Feign 接口
     */
    @GetMapping("/feign/list")
    public R<List<Destination>> feignList() {
        return R.ok(destinationService.list());
    }
    @InnerAuth
    @GetMapping("/feign/{id}")
    public R<Destination> feignGet(@PathVariable Long id) {

        return R.ok(destinationService.getById(id));
    }
    /*查国内国外的*/
    @GetMapping("/feign/isabroad/{id}")
   public R<Boolean> isabroad(@PathVariable("id") Long id){
        List<Destination> list = destinationService.queryToasts(id);
        if (list.get(0).getId()==1){
            return R.ok(false);
        }else {
            return R.ok(true);
        }

    }
    @GetMapping("/dataInit")
    public R<?> dataInit() {
        for (Destination destination : destinationService.list()) {
            DestinationES es = new DestinationES();
            //同名属性拷贝
            BeanUtils.copyProperties(destination, es);
            remoteDestinationEsService.initDestinationEs(es, SecurityConstants.INNER);


        }
        return R.ok("攻略数据初始化完成");

    }
}
